#include "customwidget.h"

#include <QPainter>
#include <QStyleOption>
#include <QPainterPath>
#include "common.h"

CustomWidget::CustomWidget(QWidget *parent)
    : QWidget(parent)
{
    this->setProperty("useSystemStyleBlur",true);
    this->setAttribute(Qt::WA_TranslucentBackground,true);
    this->initGsetting();
}

CustomWidget::~CustomWidget()
{
}

void CustomWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter p(this);
    // p.setRenderHint(QPainter::Antialiasing);  // 反锯齿;
    QPainterPath rectPath;
    rectPath.addRoundedRect(this->rect(), 0, 0); // 左上右下
//    double tran=m_pGsettingControlTrans->get("transparency").toDouble()*255;
    QStyleOption opt;
    opt.init(this);

    QColor mainColor;

    if (QColor(255,255,255) == opt.palette.color(QPalette::Base) || QColor(248,248,248) == opt.palette.color(QPalette::Base)) {
        mainColor = QColor(242, 242, 242, m_tran);
    } else {
        mainColor = QColor(20, 20, 20, m_tran);
    }

    p.fillPath(rectPath,QBrush(mainColor));
    QWidget::paintEvent(event);
}

void CustomWidget::changeTrans()
{
    m_tran = m_pGsettingControlTrans->get(TRANSPARENCY).toDouble() * 255;
    this->update();
}

//读取主题配置文件
void CustomWidget::initGsetting()
{
    if (QGSettings::isSchemaInstalled(GS_THEME_PERSONALISE)) {
        m_pGsettingControlTrans = new QGSettings(GS_THEME_PERSONALISE);
        connect(m_pGsettingControlTrans,&QGSettings::changed, this, [=] (const QString &key){
            if (key == TRANSPARENCY) {
                changeTrans();
            }
        });
    }
    changeTrans();
    return;

}
